home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / Libraries / SAT 2.3a1 / Misc / TransSkel / TransSkel.p < prev   
Encoding:
Text File  |  1994-11-02  |  51.3 KB  |  1,705 lines  |  [TEXT/PJMM]

  1. {    TransSkel version 2.00 - Transportable application skeleton}
  2.  
  3. {    TransSkel is public domain and was originally written in LightSpeed C by:}
  4.  
  5. {            Paul DuBois}
  6. {            Wisconsin Regional Primate Research Center}
  7. {            1220 Capital Court}
  8. {            Madison WI  53706  USA}
  9.  
  10. {    UUCP:    [allegra,ihnp4,seismo]!uwvax!rhesus!dubois }
  11. {    ARPA:    dubois@rhesus.primate.wisc.edu}
  12. {    The Pascal Version of TransSkel is public domain and was ported and changed by        }
  13.  
  14. {            Owen Hartnett            }
  15. {            Ωhm Software            }
  16. {            163 Richard Drive        }
  17. {            Tiverton, RI 02878        }
  18.  
  19. {    CSNET:    omh@cs.brown.edu.CSNET                                             }
  20. {    ARPA:        omh%cs.brown.edu                                                    }
  21. {    UUCP:        [ihnp4,allegra]!brunix !omh                                            }
  22.  
  23. {    This version of TransSkel written for Lightspeed Pascal.  Lightspeed Pascal is a}
  24. {    trademark of:}
  25. {            THINK Technologies, Inc}
  26. {            420 Bedford Street  Suite 350}
  27. {            Lexington, MA  02173  USA}
  28.  
  29. {  History}
  30. {  06/13/86    Beta version. (pd) }
  31. {  08/27/86    Version number changed to 1.01.(pd)}
  32. {              v1.0 DoGrow bug fixed - the port at the point of the}
  33. {              InvalRect could have been anything; the fix is to set}
  34. {              the port to the grown window first.  This also explains}
  35. {              why the kludge to DoActivate in v1.0 worked.(pd)}
  36. {  10/02/86    Version number changed to 1.02, as a result of adding}
  37. {            modifications by David W. Berry (well!dwb@lll-lcc.arpa)}
  38. {            for supporting window zooming.  Also used his modifications}
  39. {            for supporting modeless dialogs (though not in the same}
  40. {            form).  Dialogs can be #define'd on or off.(pd)}
  41.  
  42. {12/ 28 / 86 Version number changed to 1.03 . Modified to work under LightspeedC v . 2.01 }
  43. {            - took out definitions for window zooming stuff , as it is now supported by the compiler}
  44. {             directly . Also declared DoZoom static , fixing an oversight . ( pd )}
  45. { 01 / 18 / 86 Put a SetPort into DoZoom - ZoomWindow requires port to be}
  46. {            set to window being zoomed . ( pd )}
  47. { 02 / 05 / 86 Version number changed to 1.04 . Big change : port setting behavior made explicit}
  48. {             - the only persistant switch occurs when a window comes active . This changes }
  49. {            underlying programming model ( see manual for detailed discussion ) . Thanks to}
  50. {             Duane Williams for pointing out that this should be done . Typedef 'd }
  51. {            integer/long variables to Integer, Longint to facilitate coversion to other C }
  52. {            compilers . More complete type-casting done . LightspeedC does a lot of it }
  53. {            automatically , other compilers may not . ( pd - this version never released ) }
  54. {03 / 02 / 87 Fixed bug whereby clicks in drag region of non - active windows may not bring }
  55. {            window to front . Seems to be due to DragWindow calling StillDown to see    if mouse is still }
  56. {            down .    If the machine was busy    otherwise when click occurred and }
  57. {            mouse already up when DragWindow is called , the click ends up being ignored . }
  58. {            Thanks to Roger Humphrey for finding this one . }
  59.  
  60. {* * * Changes implemented first by omh to Pascal Version}
  61.  
  62. { 12/24/86  Finished first Pascal version.   Dialogs cannot be defined off.     (omh)}
  63.  
  64. {4 / 18 / 87 Changed Desk Accessory code so it 's more tolerant of memory}
  65. {            conditions for desk accessories . ( omh ) }
  66. {7 / 12 / 87 Added "cache " code to GetWDHandler . Now TransSkel figures }
  67. {            that an event is most likely to occur for the same window as the previous }
  68. {            event . Thus the WindowPtr and WDHandle for events are cached and examined }
  69. {            to avoid searching through the handler list . ( omh ) }
  70. {7 / 12 / 87 Excised the notorious "SetPort "excess . As pointed out by Duane Williams ,}
  71. {             SetPort traps abounded unnecessarily in version 1.02 . These have been eliminated }
  72. {            now with two exceptions . First , the port is set when a window handler }
  73. {            is installed . The justification for this is that when a handler is installed , it }
  74. {            is likely that further processing will be done on it immediately . The application gets }
  75. {            control immediately after the handler is installed anyway , so this behavior can be manually }
  76. {            overridden where necessary . Second , when a window is activated , the port is}
  77. {            set to it . This follows the model of keeping the port in sync with the }
  78. {            active window . ( omh ) }
  79. {7 / 14 / 87 Added grow zone function installation    and MoreMasters to SkelInit , }
  80. {            which now requires two parameters . The first indicates the number of times to call }
  81. {            MoreMasters . The second is a ProcPtr indicating a user - supplied grow zone}
  82. {            function to be called when memory problems occur .  If nil , no grow zone}
  83. {            function is    installed . ( omh ) }
  84. {7 / 14 / 87 SkelMenu , SkelWindow , and SkelDialog now return zero or non - zero to indicate }
  85. {            failure or success of handler allocation . This could break * all * previous TransSkel }
  86. {            applications ( as will the change to SkelInit , above . Please see the section "How to }
  87. {            adapt old TransSkel to New "    in the manual    for detailed specifications on }
  88. {            how to convert your old programs . TransSkel becomes more memory conscious}
  89. {            with these changes . The functions SkelMenu , SkelWindow , and }
  90. {            SkelDialog are the only routines which actually allocate memory . Since they may be }
  91. {            called at any time , knowing that you have enough memory becomes important . Thus , }
  92. {            these routines return a value to indicate what happened .  If they return zero ,}
  93. {             then memory allocation failed . ( omh ) }
  94. {10 / 21 / 87 Added another parameter to SkelMenu:  drawBar: Boolean . This tells SkelMenu }
  95. {            whether to draw the menu bar after adding the Menu . This is done to eliminate }
  96. {            the menus popping up one at a time . Simply call SkelMenu  with drawBar false}
  97. {            until the    last time you call SkelMenu , then call it (for the last menu )}
  98. {            with drawBar true . ( omh ) }
  99. {10 / 26 / 87 Removed declarations for zoom - in and zoom - out . Added Pascal }
  100. {            changes ( above ) to C version . ( omh )}
  101.  
  102. {    02 / 02 / 88 Merged pd 's 1.04 changes with those of omh, above, to create}
  103. {            release version 2.0 . Fixed bug whereby cmd - key equivalents}
  104. {            for menu selections would execute twice if DA window in front . Thanks }
  105. {            to Don Fredkin and Julian Vrieslander for finding this one , and to Don for the}
  106. {             best fix . ( pd ) }
  107. { 10/28/88 Removed all New Rom calls. }
  108. { 10/28/88 Added support for conditional compilation for dialogs and MPW support.  By setting }
  109. {            the Think_pascal flag to false, TransSkel will run under MPW. Now correctly written for LSP 2.0}
  110.  
  111. {Some fixes done later by Ingemar Ragnemalm. Search for "Ingemar" fo find these.}
  112. {They include some bug fix and hierarcical menu support (call SkelHMenu instead of SkelMenu}
  113. {for those menus).}
  114.  
  115. {New fix 18/9-93: Added a filter proc for dialogs, to allow special event processing before}
  116. {an event is passed to IsDialogEvent, when a modeless dialog is in the front. This makes it possible}
  117. {to use user items in modeless dialogs, and to handle return/enter.}
  118.  
  119. {Added WNE-support. Use SkelSetSleep and SkelSetMouseRgn if you need them. /Ingemar}
  120.  
  121. {Added FindWindowByRefcon, a routine that is useful for certain multi-window appplications.}
  122.  
  123. {july -94: Plugged in suspend/resume-handlers, conforming with TransSkel 3.0. (Handler should}
  124. {take a boolean as parameter. If the boolean is true, the application was resumed, otherwise it was}
  125. {suspended.) Processes Apple Events by calling AEProcessAppleEvent for you.}
  126.  
  127. unit TransSkel;
  128.  
  129. interface
  130.  
  131. {$SETC supportDialogs:= true }
  132.                     { Set to false to disallow modeless dialog support and save code space }
  133.                 { Set to false to have SkelInit call QuickDraw Inits: InitGraf, InitDialog, etc. }
  134.  
  135. {$IFC UNDEFINED THINK_PASCAL}
  136.  
  137.     uses
  138.         Memtypes, Quickdraw, OSIntf, ToolIntf, PackIntf;
  139. {$ENDC}
  140.  
  141.     procedure SkelInit (noMasters: integer; myGrowZone: ProcPtr);
  142.     procedure SkelMain;
  143.     procedure SkelWhoa;
  144.     procedure SkelClobber;
  145.     function SkelMenu (theMenu: MenuHandle; pSelect: ProcPtr; pClobber: ProcPtr; DrawBar: Boolean): Boolean;
  146.     function SkelHMenu (theMenu: MenuHandle; pSelect: ProcPtr; pClobber: ProcPtr): Boolean; {Added by Ingemar 22/8 -93}
  147.     procedure SkelRmveMenu (theMenu: MenuHandle);
  148.     procedure SkelApple (aboutTitle: Str255; aboutProc: ProcPtr);
  149.     function SkelWindow (theWind: WindowPtr; pMouse, pKey, pUpdate, pActivate, pClose, pClobber, pIdle: ProcPtr; frontOnly: Boolean): Boolean;
  150.     procedure SkelRmveWind (theWind: WindowPtr);
  151. {$IFC supportDialogs }
  152.     function SkelDialog (theDialog: DialogPtr; pEvent, pClose, pClobber, pFilter: ProcPtr): Boolean; {pFilter added by Ingemar 18/9-93}
  153.     procedure SkelRmveDlog (theDialog: DialogPtr);
  154. {$ENDC}
  155.     procedure SkelGrowBounds (theWind: WindowPtr; hLO, vLo, hHi, vHi: integer);
  156.     procedure SkelEventMask (mask: integer);
  157.     procedure SkelGetEventMask (var mask: integer);
  158.     procedure SkelBackground (p: ProcPtr);
  159.     procedure SkelGetBackground (var p: ProcPtr);
  160.     procedure SkelEventHook (p: ProcPtr);
  161.     procedure SkelGetEventHook (var p: ProcPtr);
  162. {$IFC supportDialogs }
  163.     procedure SkelDlogMask (mask: integer);
  164.     procedure SkelGetDlogMask (var mask: integer);
  165. {$ENDC}
  166. {Two new procedures for WNE-support, added by Ingemar 12/11-93}
  167.     procedure SkelSetSleep (newSleep: Longint);
  168.     procedure SkelSetMouseRgn (newMouseRgn: RgnHandle);
  169. {Utility function, added by Ingemar 13/7-94}
  170.     function FindWindowByRefcon (theRefCon: Longint): WindowPtr;
  171. {Suspend/resume, added by Ingemar 23/7 -94}
  172.     procedure SkelSetSuspendResume (p: ProcPtr);
  173.     function SkelGetSuspendResume: ProcPtr;
  174.  
  175. implementation
  176.  
  177. {Some stuff that are here to avoid including EPPC and AppleEvents, added by Ingemar 23/7 -94}
  178.     const
  179.         kHighLevelEvent = 23;
  180.     function AEProcessAppleEvent (theEventRecord: EventRecord): OSErr;
  181.     inline
  182.         $303C, $021B, $A816;
  183.  
  184.  
  185.  
  186.     const
  187.         mBarHeight = 20;    { menu bar height.  All window sizing}
  188.  
  189.         GrowZoneSize = 4000;        {  Size of memory to be freed when GrowZone Proc called }
  190.  
  191.         defaultSleep = 5;        { Added by Ingemar dec -93. SkelInit uses this value for sleep time.}
  192.                             { The programmer can change it with SkelSetSleep }
  193.  
  194. {    This window zooming stuff may need to be removed if you use the new Rom libraries        }
  195. {    if not, then you can add zooming without the overhead of the new Rom libs.  See TrackBox    }
  196. {     routine also.                                                                                            }
  197.  
  198. {    Window and Menu handler types, constants, variables.}
  199.  
  200. {    whList and mhList are the lists of window and menu handlers.}
  201. {    whClobOnRmve and mhClobOnRmve are true if the handler disposal proc}
  202. {    is to be called when a handler is removed.  They are temporarily set}
  203. {    false when handlers are installed for windows or menus that already}
  204. {    have handlers - the old handler is removed WITHOUT calling the}
  205. {    disposal proc.}
  206.  
  207. {    Default lower limits on window sizing of 80 pixels both directions is}
  208. {    sufficient to allow text windows room to draw a grow box and scroll}
  209. {    bars without having the thumb and arrows overlap.  These values may}
  210. {    be changed if such a constraint is undesirable with SkelGrowBounds.}
  211. {    Default upper limits are for the Macintosh, not the Lisa, but are set}
  212. {    per machine in SkelInit.}
  213.  
  214.     type
  215.         WHandlerPtr = ^WHandler;
  216.         WHandlerHnd = ^WHandlerPtr;
  217.         WHandler = record
  218.                 whWind: WindowPtr;    {window/dialog to be handled    }
  219.                 whClobber: ProcPtr;    { data structure disposal proc    }
  220.                 whMouse: ProcPtr;        { mouse-click handler proc        }
  221.                 whKey: ProcPtr;        { key-click handler proc            }
  222.                 whUpdate: ProcPtr;        { update handler proc                }
  223.                 whActivate: ProcPtr;    { activate event handler proc    }
  224.                 whClose: ProcPtr;        { close "event" handler proc        }
  225.                 whIdle: ProcPtr;            { main loop proc                    }
  226. {$IFC supportDialogs }
  227.                 whEvent: ProcPtr;        { dialog event proc                }
  228.                 whFilter: ProcPtr;        { dialog filter proc ADDED BY INGEMAR 18/9 -93}
  229. {$ENDC }
  230.                 whHasGrow: Boolean;    { can window grow?                }
  231.                 whGrow: Rect;            { limits on window sizing        }
  232.                 whSized: Boolean;        { true = window was resized    }
  233.                 whFrontOnly: Boolean;    { true = idle only when active    }
  234.                 whNext: WHandlerHnd;            { next window handler            }
  235.             end;
  236.  
  237.         MHandlerPtr = ^MHandler;
  238.         MHandlerHnd = ^MHandlerPtr;
  239.  
  240.         MHandler = record
  241.                 mhID: integer;                { menu id                                    }
  242.                 mhSelect: ProcPtr;            { item selection handler proc            }
  243.                 mhClobber: ProcPtr;        { menu disposal handler proc            }
  244.                 mhNext: MHandlerHnd;        { next menu handler                        }
  245.             end;
  246.  
  247.     var
  248.         whList: WHandlerHnd;                { list of menu handlers }
  249.         whClobOnRmve: Boolean;
  250.         growRect: Rect;
  251.         mhList: MHandlerHnd;
  252.         mhClobOnRmve: Boolean;
  253.  
  254. {    Variables for default Apple menu handler.  appleID is set to 1 if}
  255. {    SkelApple is called and is the id of the Apple menu, appleAboutProc}
  256. {    is the procedure to execute if there is an About... item and it's}
  257. {    chosen from the Apple menu.  If doAbout is true, then the menu}
  258. {    contains the About... item, otherwise it's just desk accessories.}
  259.  
  260.         appleMenu: MenuHandle;
  261.         appleID: integer;
  262.         appleAboutProc: ProcPtr;
  263.         doAbout: Boolean;
  264.  
  265. {    Miscellaneous}
  266.  
  267. {    screenPort points to the window manager port.}
  268.  
  269. {    doneFlag determines when SkelMain returns.  It is set by calling}
  270. {    SkelWhoa(), which the host does to request a halt.}
  271.  
  272. {    pBkgnd points to a background procedure, to be run during event}
  273. {    processing.  Set it with SkelBackground.  If nil, there's no}
  274. {    procedure.}
  275.  
  276. {    pEvent points to an event-inspecting hook, to be run whenever an}
  277. {    event occurs.  Set it with SkelEventHook.  If nil, there's no}
  278. {    procedure.}
  279.  
  280. {    eventMask controls the event types requested in the GetNextEvent}
  281. {    call in SkelMain.}
  282.  
  283. {    diskInitPt is the location at which the disk initialization dialog}
  284. {    appears, if an uninitialized disk is inserted.}
  285.  
  286.         screenPort: GrafPtr;
  287.         doneFlag: integer;
  288.         pBkgnd: ProcPtr;
  289.         pEvent: ProcPtr;
  290.         eventMask: integer;
  291.         diskInitPt: Point;
  292.  
  293. {Added by Ingemar 12/11-93, for WNE-support:}
  294.         WNEImplemented: Boolean;
  295.         sleepTicks: Longint;
  296.         mouseRgn: rgnHandle;
  297. {…and later, for suspend/resume support:}
  298.         gSuspendResumeHandler: ProcPtr;
  299.  
  300. {$IFC supportDialogs }
  301.  
  302. {    Events that are passed to dialogs.  Others are ignored.}
  303. {    Standard mask passes , mousedown, keydown, autokey, update,}
  304. {    activate and null events.  Null events are controlled by bit 0.}
  305.  
  306.         dlogEventMask: integer;
  307. {$ENDC}
  308.         pEventflag: Boolean;
  309.  
  310. {    "caching" global variables.  previous version would search down the window }
  311. {    list for every event it found.  Now, if the event happened to the same window }
  312. {    as last time, GetWDHandler will just do a simple compare }
  313. {    and return the last window handler.  This speeds up multiple window applications }
  314. {    immensely, at only a slight cost when you activate a new window (one }
  315. {    compare!)  If you don't like it, use the old version. }
  316.  
  317.         oldWindow: WindowPtr;
  318.         oldWDHandler: WHandlerHnd;
  319.  
  320. {    Global for built in "Grow Zone" function  }
  321.  
  322.         safetyHandle: Handle;
  323.  
  324.         myDitl: packed array[0..100] of byte;
  325.  
  326. { Rather than including the entire new ROM libraries, with all the other stuff you might not use    }
  327. { I've instead included just the Zoom box stuff here.  Depending on your status, you can either    }
  328. { leave things as they are, and only use zooming from the new Rom libs, or comment out the        }
  329. { calls, and include the new Rom libraries if you want to incorporate other new Rom calls        }
  330.  
  331.  
  332. { -------------------------------------------------------------------- }
  333. {                        Internal (private) Routines                                    }
  334. { -------------------------------------------------------------------- }
  335.  
  336.  
  337.  
  338. {    Get handler associated with user or dialog window.}
  339. {    Return nil if window doesn't belong to any known handler.}
  340. {    This routine is absolutely fundamental to TransSkel.}
  341.  
  342.     function GetWDHandler (theWind: WindowPtr): WHandlerHnd;
  343.  
  344.         var
  345.             h: WHandlerHnd;
  346.     begin
  347.         h := WhList;
  348.         GetWDHandler := nil;
  349.         if theWind = oldWindow then            {  caching code     }
  350.             GetWDHandler := oldWDHandler
  351.         else
  352.             while h <> nil do
  353.                 if h^^.whWind = theWind then
  354.                     begin
  355.                         oldWindow := theWind;            { Load in new values for new window }
  356.                         oldWDHandler := h;
  357.                         GetWDHandler := h;
  358.                         h := nil;
  359.                     end
  360.                 else
  361.                     h := WHandlerHnd(h^^.whNext);
  362.     end;
  363.  
  364. { Get Handler associated with user window.  Return nil if window doesn't}
  365. {  have a Handler. }
  366.  
  367.     function GetWHandler (theWind: WindowPtr): WHandlerHnd;
  368.  
  369.         var
  370.             h: WHandlerHnd;
  371.             myPeek: WindowPeek;
  372.  
  373.     begin
  374. {BUG FIXED by Ingemar 19/9-93. This function retured garbage when passed a dialog}
  375.         h := GetWDHandler(theWind);
  376.         myPeek := WindowPeek(theWind);
  377.         GetWHandler := nil; {default, moved up by Ingemar}
  378.         if h <> nil then
  379.             begin
  380.                 if mypeek^.windowKind <> dialogKind then
  381.                     GetWHandler := h;
  382.             end;
  383.     end;
  384.  
  385. {$IFC supportDialogs }
  386.  
  387. {    Get handler associated with dialog window.}
  388. {    Return nil if window doesn't belong to any known handler.}
  389.  
  390.     function GetDHandler (theDialog: WindowPtr): WHandlerHnd;
  391.  
  392.         var
  393.             h: WHandlerHnd;
  394.             myPeek: WindowPeek;
  395.  
  396.     begin
  397. {BUG FIXED by Ingemar 19/9-93. This function retured garbage when passed a non-dialog}
  398.         h := GetWDHandler(theDialog);
  399.         myPeek := WindowPeek(theDialog);
  400.         GetDHandler := nil; {default - moved up by Ingemar}
  401.         if h <> nil then
  402.             begin
  403.                 if mypeek^.windowKind = dialogKind then
  404.                     GetDHandler := h;
  405.             end;
  406.     end;
  407. {$ENDC}
  408.  
  409. {Two new routines added by Ingemar 12/11-93 for WNE support:}
  410.     procedure SkelSetSleep (newSleep: Longint);
  411.     begin
  412.         sleepTicks := newSleep;
  413.     end;
  414.     procedure SkelSetMouseRgn (newMouseRgn: RgnHandle);
  415.     begin
  416.         mouseRgn := newMouseRgn;
  417.     end;
  418.  
  419. {Utility routine added by Ingemar july 1994. Finds a window in the window list that has the same}
  420. {refCon as the value passed. This is useful for programs where several windows share window handlers.}
  421. {(To be precise, i needed it for a program where I have several connections to other Macs over Appletalk.)}
  422.     function FindWindowByRefcon (theRefCon: Longint): WindowPtr;
  423.         var
  424.             h: WHandlerHnd;
  425.     begin
  426.         FindWindowByRefcon := nil;
  427.         h := WhList;
  428.         while h <> nil do
  429.             if WindowPeek(h^^.whWind)^.refCon = theRefCon then
  430.                 begin
  431.                     FindWindowByRefcon := h^^.whWind;
  432.                     exit(FindWindowByRefcon);
  433.                 end
  434.             else
  435.                 h := WHandlerHnd(h^^.whNext);
  436.     end;
  437.  
  438.     procedure SkelSetSuspendResume (p: ProcPtr);
  439.     begin
  440.         gSuspendResumeHandler := p;
  441.     end;
  442.     function SkelGetSuspendResume: ProcPtr;
  443.     begin
  444.         SkelGetSuspendResume := gSuspendResumeHandler;
  445.     end;
  446.  
  447. {The following procedures are Pascal "glue" that allows Pascal to call a Procedure    }
  448. { from a ProcPtr.  It is similar to (*p) () construct used in the C dialect.  Different    }
  449. { procedures are necessary for the reason of Pascal's strongly typed parameter        }
  450. { list.  Fortunately, there are not too many calls which use different param lists        }
  451.  
  452.     procedure callpMouse (thePoint: Point; theTime: longint; theMods: integer; myProc: ProcPtr);
  453.  
  454.     inline
  455.         $205f,     {movea.l  (a7)+,a0        ; (a0) is a ptr to string, 4(a0) is mode}
  456.         $4e90;
  457.  
  458.     procedure callpKey (theChar: char; theMods: integer; myProc: ProcPtr);
  459.  
  460.     inline
  461.         $205f,     {movea.l  (a7)+,a0        ; (a0) is a ptr to string, 4(a0) is mode}
  462.         $4e90;
  463.  
  464.     procedure callpEvent (theitem: integer; var theEvent: EventRecord; myProc: ProcPtr);
  465.  
  466.     inline
  467.         $205f,     {movea.l  (a7)+,a0        ; (a0) is a ptr to string, 4(a0) is mode}
  468.         $4e90;
  469.  
  470.     function callotherEvent (var theEvent: EventRecord; myProc: ProcPtr): Boolean;
  471.  
  472.     inline
  473.         $205f, $4e90;
  474.  
  475.  
  476.     procedure callpBoolean (myBool: Boolean; myProc: ProcPtr);
  477.  
  478. { Two calls use Booleans as one parameter arguments.  This procedure handles    }
  479. { both of them.                                                                            }
  480.  
  481.     inline
  482.         $205f,     {movea.l  (a7)+,a0        ; (a0) is a ptr to string, 4(a0) is mode}
  483.         $4e90;
  484.  
  485.     procedure callpInt (myInt: integer; myProc: ProcPtr);
  486.  
  487. { Two calls use Booleans as one parameter arguments.  This procedure handles    }
  488. { both of them.                                                                            }
  489.  
  490.     inline
  491.         $205f,     {movea.l  (a7)+,a0        ; (a0) is a ptr to string, 4(a0) is mode}
  492.         $4e90;
  493.  
  494.     procedure callpMenu (myMenu: MenuHandle; myProc: ProcPtr);
  495.  
  496. { Handle removeal of menus.     }
  497.  
  498.     inline
  499.         $205f,     {movea.l  (a7)+,a0        ; (a0) is a ptr to string, 4(a0) is mode}
  500.         $4e90;
  501.  
  502.     procedure callpnoarg (myProc: ProcPtr);
  503.  
  504. { For all the Procedures that are called with no arguments                            }
  505.  
  506.     inline
  507.         $205f,     {movea.l  (a7)+,a0        ; (a0) is a ptr to string, 4(a0) is mode}
  508.         $4e90;
  509.  
  510.  
  511. {ADDED BY INGEMAR 18/9-93 to support new dialog filters}
  512. {IS IT POSSIBLE to use this for functions?}
  513.     procedure callpfilter (theDialog: DialogPtr; var theEvent: EventRecord; var result: Boolean; myProc: ProcPtr);
  514.     inline
  515.         $205f,     {movea.l  (a7)+,a0        ; (a0) is a ptr to string, 4(a0) is mode}
  516.         $4e90;
  517.  
  518. {    General menu-handler.  Just passes selection to the handler's}
  519. {    select routine.  If the select routine is nil, selecting items from}
  520. {    the menu is a nop.}
  521.  
  522.     procedure DoMenuCommand (command: longint);
  523.  
  524.         var
  525.             menu: integer;
  526.             item: integer;
  527.             mh: MHandlerHnd;
  528.             p: ProcPtr;
  529.  
  530.     begin
  531.         menu := HiWord(command);
  532.         item := LoWord(command);
  533.         mh := mhList;
  534.         while (mh <> nil) do
  535.             begin
  536.                 p := mh^^.mhSelect;
  537.                 if ((menu = mh^^.mhID) and (p <> nil)) then
  538.                     begin
  539.                         callpInt(item, p);
  540.                         mh := nil;
  541.                     end
  542.                 else
  543.                     mh := mh^^.mhNext;
  544.             end;
  545.         HiliteMenu(0);
  546.     end;
  547.  
  548. {    Apple menu handler}
  549.  
  550. {    DoAppleItem:  If the first item was chosen, and there's an "About..."}
  551. {    item, call the procedure associated with it (if not nil).  If there}
  552. {    is no "About..." item or the item was not the first one, then open}
  553. {    the associated desk accessory.  The port is saved and restored}
  554. {    because OpenDeskAcc does not always preserve it correctly.}
  555.  
  556. {    DoAppleClobber disposes of the Apple menu.}
  557.  
  558.     procedure DoAppleItem (item: integer);
  559.  
  560.         var
  561.             curPort: GrafPtr;
  562.             str: Str255;
  563.             ignore: integer;
  564.             h: Handle;
  565.  
  566.     begin
  567.         if doAbout and (item = 1) then
  568.             begin
  569.                 if appleAboutProc <> nil then
  570.                     callpnoarg(appleAboutProc);
  571.             end
  572.         else
  573.             begin
  574.                 GetPort(curPort);
  575.                 GetItem(appleMenu, item, str);
  576.                 SetResLoad(false);
  577.                 h := GetNamedResource('DRVR', str);
  578.                 SetResLoad(true);
  579.                 if h <> nil then
  580.                     begin
  581.                         ResrvMem(SizeResource(h) + $1000);
  582.                         ignore := OpenDeskAcc(str);
  583.                     end;
  584.                 SetPort(curPort);
  585.             end;
  586.     end;
  587.  
  588.     procedure DoAppleClobber;
  589.     begin
  590.         DisposeMenu(appleMenu);
  591.     end;
  592.  
  593. { --------------------------------------------------------------------     }
  594. {                        Window-handler routing routines                            }
  595. {                                                                                        }
  596. {    Each routine sets the port to the handler's window before executing        }
  597. {    the handler procedure.                                                            }
  598. { --------------------------------------------------------------------     }
  599.  
  600.  
  601. {    Pass local mouse coordinates, click time, and the modifiers flag}
  602. {    word to the handler.  Should not be necessary to set the port, as}
  603. {       the click is passed to the active window's hander. }
  604.  
  605.     procedure DoMouse (h: WHandlerHnd; theEvent: EventRecord);
  606.  
  607.         var
  608.             p: ProcPtr;
  609.             thePt: Point;
  610.  
  611.     begin
  612.         if (h <> nil) then
  613.             begin
  614.                 p := h^^.whMouse;
  615.                 if p <> nil then
  616.                     begin
  617.                         thePt := theEvent.where;
  618.                         GlobalToLocal(thePt);
  619.                         callpMouse(thePt, theEvent.when, theEvent.modifiers, p);
  620.                     end;
  621.             end;
  622.     end;
  623.  
  624. {    Pass the character and the modifiers flag word to the handler.}
  625. {    Should not be necessary to set the port, as the  click is passed to the}
  626. {    active window's handler. }
  627.  
  628.     procedure DoKey (h: WHandlerHnd; ch: char; mods: integer);
  629.         var
  630.             p: ProcPtr;
  631.  
  632.     begin
  633.         if h <> nil then
  634.             begin
  635.                 p := h^^.whKey;
  636.                 if p <> nil then
  637.                     callpKey(ch, mods, p);
  638.             end;
  639.     end;
  640.  
  641. {    Call the window updating procedure, passing to it an indicator whether the}
  642. {    window has been resized or not.  Then clear the flag, assuming the update}
  643. {    proc took whatever action was necessary to respond to resizing.}
  644. {}
  645. {    If the handler doesn't have any update proc, the Begin/EndUpdate stuff}
  646. {    is still done, to clear the update region.  Otherwise the Window Manager }
  647. {    will keep generating update events for the window, stalling updates of}
  648. {    other windows.    }
  649.  
  650. {    Make sure to save and restore the port, as it's not always the active window}
  651. {    that's updated.    }
  652.  
  653.     procedure DoUpdate (h: WHandlerHnd);
  654.  
  655.         var
  656.             rh: WhandlerHnd;
  657.             p: ProcPtr;
  658.             updPort, tmpPort: GrafPtr;
  659.  
  660.     begin
  661.         rh := h;
  662.         if rh <> nil then
  663.             begin
  664.                 GetPort(tmpPort);
  665.                 updPort := rh^^.whWind;
  666.                 SetPort(updPort);
  667.                 BeginUpdate(updPort);
  668.                 p := rh^^.whUpdate;
  669.                 if p <> nil then
  670.                     begin
  671.                         callpBoolean(rh^^.whSized, p);
  672.                         rh^^.whSized := false;
  673.                     end;
  674.                 EndUpdate(updPort);
  675.                 SetPort(tmpPort);
  676.             end;
  677.     end;
  678.  
  679. {    Pass activate/deactivate notification to handler.  On activate, set the port to}
  680. {    the window coming active    }
  681.  
  682.     procedure DoActivate (h: WHandlerHnd; active: Boolean);
  683.  
  684.         var
  685.             p: ProcPtr;
  686.  
  687.     begin
  688.         if h <> nil then
  689.             begin
  690.                 if active then
  691.                     SetPort(h^^.whWind);
  692.                 p := h^^.whActivate;
  693.                 if p <> nil then
  694.                     callpBoolean(active, p);
  695.             end
  696.     end;
  697.  
  698. {    Execute a window handler's close proc.  The close box for handlers}
  699. {    for temp windows that want to remove themselves when the window}
  700. {    is closed can call SkelRmveWind to dispose of the window}
  701. {    and remove the handler from the window handler list.  Thus, windows}
  702. {    may be dynamically created and destroyed without filling up the}
  703. {    handler list with a bunch of invalid handlers.}
  704.  
  705. {    If the handler doesn't have a close proc, just hide the window.}
  706. {    The host should provide some way of reopening the window (perhaps}
  707. {    a menu selection).  Otherwise the window will be lost from user}
  708. {    control if it is hidden, since it won't receive user-initiated events.}
  709.  
  710. {    Since the close box of only the active window may be clicked, it}
  711. {    is not necessary to set the port . }
  712.  
  713. {    This is called both for regular and dialog windows.}
  714.  
  715.     procedure DoClose (h: WHandlerHnd);
  716.  
  717.         var
  718.             rh: WHandlerHnd;
  719.             p: ProcPtr;
  720.     begin
  721.         rh := h;
  722.         if rh <> nil then
  723.             begin
  724.                 p := rh^^.whClose;
  725.                 if (p <> nil) then
  726.                     callpnoarg(p)
  727.                 else
  728.                     HideWindow(rh^^.whWind);
  729.             end;
  730.     end;
  731.  
  732. {    Execute a window Handler's clobber proc.  This is called both for regular and dialog windows.}
  733. {    Must save, set and restore port, since any window (not just active one) may be clobbered    }
  734. {    at any time.}
  735. {}
  736. {    Don't need to check whether handler is nil, as in other handler procedures, since this is only}
  737. {    called by SkelRmveWind with a known valid handler.     }
  738.  
  739.     procedure DoClobber (h: WHandlerHnd);
  740.  
  741.         var
  742.             p: ProcPtr;
  743.             curPort: Grafptr;
  744.     begin
  745.         if (h <> nil) then
  746.             begin
  747.                 GetPort(curPort);
  748.                 SetPort(h^^.whWind);
  749.                 p := h^^.whClobber;
  750.                 if p <> nil then
  751.                     callpnoarg(p);
  752.                 SetPort(curPort);
  753.             end;
  754.     end;
  755.  
  756. {$IFC supportDialogs }
  757.  
  758. {    Handle event if it's for a dialog.  The event must be one of}
  759. {    those that is passed to dialogs according to dlogEventMask.}
  760. {    This mask can be set so that disk-inserts, for instance, don't}
  761. {    get eaten up.}
  762.  
  763.     function DoDialog (theEvent: EventRecord): Boolean;
  764.  
  765.         var
  766.             dh: WHandlerHnd;
  767.             theDialog: DialogPtr;
  768.             myDPeek: DialogPeek;
  769.             what: integer;
  770.             item: integer;
  771.             tmpPort: GrafPtr;
  772.             ignore: Boolean;
  773.             testme: longint;
  774.             hasfilter, filtered: Boolean;
  775.  
  776.     begin
  777.  
  778. {    handle command keys before they get to IsDialogEvent}
  779.  
  780.         what := theEvent.what;
  781.         testme := BitShift(longint(1), what);
  782.         testme := BitAnd(testme, longint(dlogEventMask));
  783.         if (((what = keydown) or (what = autokey)) and (BitAnd(theEvent.modifiers, cmdkey) <> 0)) then
  784. { Bugfix by Ingemar above! }
  785.             begin
  786.                 DoMenuCommand(MenuKey(Char(BitAnd(theEvent.message, charCodeMask))));
  787.                 DoDialog := true;
  788.             end
  789. {Filter procedure, Added by Ingemar 18/9 -93:}
  790.         else
  791.             begin
  792. {Check if theDialog has whFilter!}
  793. {Bugfix 941026: I used FrontWindow, which was really silly since that won't work}
  794. {for update events!}
  795.                 theDialog := DialogPtr(theEvent.message);
  796.                 dh := WHandlerHnd(GetDHandler(theDialog));
  797.                 filtered := false;
  798.                 hasfilter := dh <> nil;
  799.                 if hasfilter then
  800.                     hasfilter := dh^^.whFilter <> nil;
  801.                 if hasfilter then
  802.                     CallPFilter(theDialog, theEvent, filtered, dh^^.whFilter);
  803.                 DoDialog := filtered;
  804.                 if not filtered then
  805. {end of filter proc handling}
  806. {else}
  807.                     if testme > 0 then
  808.                         if IsDialogEvent(theEvent) then
  809.                             begin
  810.                                 if DialogSelect(theEvent, theDialog, item) then
  811.                                     begin
  812.                                         dh := WHandlerHnd(GetDHandler(theDialog));
  813.                                         if (dh <> nil) then
  814.                                             if (dh^^.whEvent <> nil) then
  815.                                                 begin
  816.                                                     GetPort(tmpPort);
  817.                                                     SetPort(theDialog);
  818.                                                     callpEvent(item, theEvent, dh^^.whEvent);
  819.                                                     SetPort(tmpPort);
  820.                                                 end;
  821.                                     end;
  822.                                 DoDialog := true;
  823.                             end
  824.                         else
  825.                             DoDialog := false;
  826.             end; {to match begin added with filter above}
  827.     end;
  828. {$ENDC}
  829.  
  830. { -------------------------------------------------------------------- }
  831. {                            Event-handling routines                        }
  832. { -------------------------------------------------------------------- }
  833.  
  834. {    Have either sized or zoomed the window.  Invalidate it to force}
  835. {    an update and set the 'resized' flag in the window handler true.}
  836. {    The port is assumed to be set to the port that changed size.        }
  837.  
  838.     procedure TriggerUpdate (h: WHandlerHnd; thePort: GrafPtr);
  839.  
  840.     begin
  841.         InvalRgn(thePort^.visRgn);
  842. {Changed by Ingemar 1/4-94. Was: InvalRect(thePort^.portRect);}
  843. {This is ok for rectangular windows, but all windows aren't rectangular!}
  844.         if (h <> nil) then
  845.             begin
  846.                 h^^.whSized := true;
  847.             end;
  848.     end;
  849.  
  850. {    Size a window.  If the window has a handler, use the grow limits}
  851. {    in the handler record, otherwise use the defaults.}
  852.  
  853. {    The portRect is invalidated to force an update event.    The handler's}
  854. {    update procedure should check the parameter passed to it to check}
  855. {    whether the window has changed size, if it needs to adjust itself to}
  856. {    the new size.  THIS IS A CONVENTION.  Update procs must notice grow}
  857. {    "events", there is no procedure specifically for such events.}
  858.  
  859. {    The clipping rectangle is not reset.  If the host application}
  860. {    keeps the clipping set equal to the portRect or something similar,}
  861. {    then it will have to arrange to treat window growing with more}
  862. {    care.}
  863. {}
  864. {    Since the grow region of only the active window may be clicked, it should}
  865. {    not be necessary to set the port.}
  866.  
  867.     procedure DoGrow (h: WHandlerHnd; thePort: GrafPtr; StartPt: Point);
  868.  
  869.         var
  870.             r: Rect;
  871.             growRes: longint;
  872.  
  873.     begin
  874.         if (h <> nil) then
  875.             begin
  876.                 r := h^^.whGrow;
  877.             end
  878.         else
  879.             r := growRect;
  880.         growRes := GrowWindow(thePort, startPt, r);
  881.         if growRes <> 0 then
  882.             begin
  883.                 SizeWindow(thePort, LoWord(growRes), HiWord(growRes), false);
  884.                 TriggerUpdate(h, thePort);
  885.             end;
  886.     end;
  887.  
  888.  
  889. {    Zoom the current window.  Very similar to DoGrow}
  890. {    Since the zoombox of only the active window may be clicked, it should not be necessary}
  891. {    to set the port.    }
  892.  
  893.     procedure DoZoom (h: WHandlerHnd; thePort: GrafPtr; partcode: integer);
  894.  
  895.     begin
  896.         ZoomWindow(thePort, partcode, false);
  897.         TriggerUpdate(h, thePort);
  898.     end;
  899.  
  900. {    General event handler}
  901.  
  902.     procedure DoEvent (theEvt: Eventrecord);
  903.  
  904.         var
  905.             theEvent: EventRecord;
  906.             evtPt: Point;
  907.             evtPort: GrafPtr;
  908.             evtPart: integer;
  909.             evtChar: char;
  910.             evtMods: integer;
  911.             h: WHandlerHnd;
  912.             r: Rect;
  913.             ignore: integer;
  914.  
  915.     begin
  916.         theEvent := theEvt;
  917. {$IFC supportDialogs }
  918.         if not (DoDialog(theEvent)) then
  919. {$ENDC}
  920.             begin
  921.                 evtPt := theEvent.where;
  922.                 evtMods := theEvent.modifiers; {Bug fixed by Ingemar 941027 - this statement was missing}
  923.                 case theEvent.what of
  924.                     nullEvent: 
  925.                         ;
  926.  
  927. {    Mouse click.  Get the window that the click occurred in, and the}
  928. {    part of the window.    Get WDHandler is called here, not GetWHandler, since}
  929. {    we need the handler for a window which might turn out to be a dialog window,}
  930. {    e.g., if the click is in a close box.}
  931.  
  932.                     mouseDown: 
  933.                         begin
  934.                             evtPart := FindWindow(evtPt, evtPort);
  935.                             h := GetWDHandler(evtPort);
  936.                             case evtPart of
  937.  
  938. {    Click in a desk accessory window.  Pass back to the system.}
  939.  
  940.                                 inSysWindow: 
  941.                                     SystemClick(theEvent, evtPort);
  942.  
  943. {    Click in menu bar.  Track the mouse and execute selected command,}
  944. {    if any.}
  945.  
  946.                                 inMenuBar: 
  947.                                     DoMenuCommand(MenuSelect(evtPt));
  948.  
  949. {    Click in grow box.  Resize window.}
  950.  
  951.                                 inGrow: 
  952.                                     DoGrow(h, evtPort, evtPt);
  953.  
  954. {    Click in title bar.  Drag the window around.  Leave at least}
  955. {    4 pixels visible in both directions.    Bug fix: The window, if not front, is}
  956. {    selected first to make sure it's at least activated (unless the command key is down - see Inside}
  957. {    Macintosh).  DragWindow seems to call StillDown first, so that clicks in drag regions while}
  958. {    machine is busy don't otherwise bring window to front if the mouse is already up by the time}
  959. {    DragWindow is called.}
  960.  
  961. {BUG??? Where is evtmods assigned???}
  962.  
  963.                                 inDrag: 
  964.                                     begin
  965.                                         if (evtPort <> FrontWindow) and (BitAnd(evtmods, cmdKey) = 0) then
  966.                                             SelectWindow(evtPort);
  967.                                         r := screenPort^.portRect;
  968.                                         r.top := r.top + mBarHeight;        { Skip down past menu bar    }
  969.                                         InsetRect(r, 4, 4);
  970.                                         DragWindow(evtPort, evtPt, r);
  971.                                     end;
  972.  
  973. {    Click in close box.  Call the close proc if the window has one.}
  974.  
  975.                                 inGoAway: 
  976.                                     if (TrackGoAway(evtPort, evtPt)) then
  977.                                         DoClose(GetWDHandler(evtPort));
  978.  
  979. {    Click in content region.  If the window wasn't frontmost (active),}
  980. {    just select it, otherwise pass the click to the window's mouse}
  981. {    click handler.}
  982.  
  983.                                 inContent: 
  984.                                     if (evtPort <> FrontWindow) then
  985.                                         SelectWindow(evtPort)
  986.                                     else
  987.                                         DoMouse(h, theEvent);
  988.  
  989. {    Click in zoom box.  Track the click and then zoom the window if}
  990. {    necessary}
  991.  
  992.                                 inZoomin, inZoomOut: 
  993.                                     if (TrackBox(evtPort, evtPt, evtPart)) then
  994.                                         DoZoom(h, evtport, evtPart);
  995.                                 otherwise
  996.                                     ;
  997.                             end;{mousedown}
  998.                         end;
  999.  
  1000. {    Key event.  If the command key was down, process as menu item}
  1001. {    selection, otherwise pass the character and the modifiers flags}
  1002. {    to the active window's key handler.}
  1003.  
  1004. {    If dialogs are supported, there's no check for command-key}
  1005. {    equivalents, since that would have been checked in DoDialog.}
  1006.  
  1007.                     keydown, autokey: 
  1008.                         begin
  1009.                             evtChar := char(BitAnd(theEvent.message, charCodeMask));
  1010.                             evtMods := theEvent.modifiers;
  1011.                             if BitAnd(evtMods, cmdKey) > 0 then
  1012.                                 DoMenuCommand(menuKey(evtChar))
  1013.                             else
  1014.                                 DoKey(GetWHandler(FrontWindow), evtChar, evtMods);
  1015.                         end;
  1016.  
  1017. {    Update a window.}
  1018.  
  1019.                     updateEvt: 
  1020.                         DoUpdate(GetWHandler(WindowPtr(theEvent.message)));
  1021.  
  1022. {    Activate or deactivate a window.}
  1023.  
  1024.                     activateEvt: 
  1025.                         DoActivate(GetWHandler(WindowPtr(theEvent.message)), (BitAnd(theEvent.modifiers, activeFlag) <> 0));
  1026.  
  1027. {    handle inserts of uninitialized disks}
  1028.  
  1029.                     diskEvt: 
  1030.                         if (HiWord(theEvent.message) <> noErr) then
  1031.                             begin
  1032.                                 DILoad;
  1033.                                 ignore := DIBadMount(diskInitPt, theEvent.message);
  1034.                                 DIUnload;
  1035.                             end;
  1036.  
  1037. { Handle suspend/resume and Apple Events, added by Ingemar 23/7 -94}
  1038.  
  1039.                     OSevt: 
  1040.                         if gSuspendResumeHandler <> nil then
  1041.                             if BAND(BROTL(theEvent.message, 8), $FF) = SuspendResumeMessage then
  1042.                                 CallPBoolean(BAnd(theEvent.message, 1) <> 0, gSuspendResumeHandler);
  1043.  
  1044. { Handle Apple Events, added by Ingemar 23/7 -94}
  1045.  
  1046.                     kHighLevelEvent: 
  1047.                         if AEProcessAppleEvent(theEvent) <> noErr then
  1048.                             ;
  1049.  
  1050.                     otherwise
  1051.                 end;
  1052.             end;
  1053.     end;
  1054.  
  1055. { -------------------------------------------------------------------- }
  1056. {                        Interface (public) Routines                        }
  1057. { -------------------------------------------------------------------- }
  1058.  
  1059.  
  1060. {    Initialize the various Macintosh Managers.}
  1061. {    Set default upper limits on window sizing.}
  1062. {    FlushEvents does NOT toss disk insert events, so that disks}
  1063. {    inserted while the application is starting up don't result}
  1064. {    in dead drives.}
  1065. {    NoMasters is the number of times to call MoreMasters.  gzProc is the address of a user - provided}
  1066. {    grow zone function procedure to call if memory gets tight.  Pass nil if none to be used.    }
  1067.  
  1068.     procedure SkelInit;
  1069.         var
  1070.             i: integer;
  1071.  
  1072.     begin
  1073.  
  1074. { For non-Lightspeed Pascal users, the following inits are included as a compile time option, }
  1075. {  See the $SETC definition at the beginning of the unit.  }
  1076.  
  1077. {$IFC UNDEFINED THINK_PASCAL }
  1078.         InitGraf(@qd.thePort);
  1079.         InitFonts;
  1080.         InitWindows;
  1081.         InitMenus;
  1082.         TEInit;
  1083.         InitDialogs(nil);
  1084.         MaxApplZone;
  1085. {$ENDC}
  1086.  
  1087.         FlushEvents(everyEvent - diskMask, 0);
  1088.         for i := 1 to noMasters do
  1089.             MoreMasters;
  1090.         if myGrowZone <> nil then
  1091.             SetGrowZone(myGrowZone);
  1092.  
  1093.         InitCursor;
  1094.  
  1095. {Added by Ingemar 12/11-93, for WNE-support:}
  1096.         WNEImplemented := NGetTrapAddress($60, ToolTrap) <> NgetTrapAddress($9F, ToolTrap);
  1097.         SkelSetSleep(defaultSleep);
  1098.         SkelSetMouseRgn(nil);
  1099. {End WNE-support}
  1100.  
  1101.         whList := nil;
  1102.         whClobOnRmve := true;
  1103.         SetRect(growRect, 80, 80, 512, 342 - mBarHeight);
  1104.         mhList := nil;
  1105.         mhClobOnRmve := true;
  1106.         appleID := 0;
  1107.         appleAboutProc := nil;
  1108.         doAbout := false;
  1109.         doneflag := 0;
  1110.         pBkgnd := nil;
  1111.         pEvent := nil;
  1112.         pEventflag := false;
  1113.         eventmask := everyEvent;
  1114.         diskInitPt.v := 120;
  1115.         diskInitPt.h := 100;
  1116. {$IFC supportDialogs }
  1117.         dlogEventMask := $16f;
  1118. {$ENDC}
  1119.  
  1120. {    Set upper limits of window sizing to machine screen size.  Allow}
  1121. {    for the menu bar.}
  1122.  
  1123.         GetWMgrPort(screenport);
  1124.         growRect.right := screenPort^.portRect.right;
  1125.         growRect.bottom := screenPort^.portRect.bottom - mBarHeight;
  1126.  
  1127. {    Set caching global variables to nil }
  1128.  
  1129.         oldWindow := nil;
  1130.         oldWDHandler := nil;
  1131.     end;
  1132.  
  1133. {    Main loop.}
  1134.  
  1135. {    Task care of DA's with SystemTask.}
  1136. {    Run background task if there is one.}
  1137. {    If there is an event, check for an event hook.  If there isn't}
  1138. {    one defined, or if there is but it returns false, call the}
  1139. {    general event handler.  (Hook returns true if TransSkel should}
  1140. {    ignore the event.)}
  1141. {    If no event, call the "no-event" handler for the front window and for}
  1142. {    any other windows with idle procedures that are always supposed}
  1143. {    to run.  This is done in such a way that it is safe for idle procs}
  1144. {    to remove the handler for their own window if they want (unlikely,}
  1145. {    but...)  This loop doesn't check whether the window is really}
  1146. {    a dialog window or not, but it doesn't have to, because such}
  1147. {    things always have a nil idle proc.}
  1148. {    }
  1149. {    doneFlag is reset upon exit.  This allows it to be called}
  1150. {    repeatedly, or recursively.}
  1151.  
  1152. {    Null events are looked at (in SkelMain)}
  1153. {    and passed to the event handler.  This is necessary to make sure}
  1154. {    DialogSelect gets called repeatedly, or the caret won't blink if}
  1155. {    a dialog has any editText items.  Null events are not passed to any event-inspecting hook that may}
  1156. {    be installed.}
  1157.  
  1158.     procedure SkelMain;
  1159.  
  1160.         var
  1161.             theEvent: EventRecord;
  1162.             wh, wh2: WHandlerHnd;
  1163.             w: WindowPtr;
  1164.             haveEvent, testpevent, testbool: Boolean;
  1165.             tmpPort: GrafPtr;
  1166.             p: ProcPtr;
  1167.  
  1168.     begin
  1169.         while (doneFlag = 0) do
  1170.             begin
  1171.                 if WNEImplemented then {Added by Ingemar 12/11-93, for WNE-support:}
  1172.                     begin
  1173.                         if (pBkgnd <> nil) then
  1174.                             callpnoarg(pBkgnd);
  1175.                         haveEvent := WaitNextEvent(eventMask, theEvent, sleepTicks, mouseRgn);
  1176.                     end
  1177.                 else
  1178.                     begin {Old way:}
  1179.                         SystemTask;
  1180.                         if (pBkgnd <> nil) then
  1181.                             callpnoarg(pBkgnd);
  1182.                         haveEvent := GetNextEvent(eventMask, theEvent);
  1183.                     end;
  1184.  
  1185.                 if (pEvent <> nil) then
  1186.                     testpevent := CallotherEvent(theEvent, pEvent)
  1187.                 else
  1188.                     testpevent := false;
  1189. { following line fixed from version 1.02 & 1.03     [but was still buggy! /Ingemar]}
  1190. {$IFC supportDialogs }
  1191.                 if (pEvent = nil) or (testpevent = false) then {haveEvent and <- This is wrong for modeless dialogs according to page 416? Changed by Ingemar 6/8 -93}
  1192. {$ELSEC}
  1193.                     if haveEvent and ((pEvent = nil) or (testpevent = false)) then { Old line. IFC'ad by Ingemar 6/8 -93}
  1194. {$ENDC}
  1195.                         DoEvent(theEvent);
  1196.                 if not haveEvent then
  1197.                     begin
  1198.                         wh := whList;
  1199.                         GetPort(tmpPort);
  1200.                         while (wh <> nil) do
  1201.                             begin
  1202.                                 wh2 := wh^^.whNext;
  1203.                                 w := wh^^.whWind;
  1204.                                 if ((w = FrontWindow) or not wh^^.whFrontOnly) then
  1205.                                     begin
  1206.                                         SystemTask;
  1207.                                         if (wh^^.whIdle <> nil) then
  1208.                                             begin
  1209.                                                 SetPort(wh^^.whWind);
  1210.                                                 p := wh^^.whIdle;
  1211.                                                 if (p <> nil) then
  1212.                                                     callpnoarg(p);
  1213.                                             end;
  1214.                                     end;
  1215.                                 wh := wh2;
  1216.                             end;
  1217.                         SetPort(tmpPort);
  1218.                     end;
  1219.             end;
  1220.         doneFlag := 0;
  1221.     end;
  1222.  
  1223. {    Tell SkelMain to stop}
  1224.  
  1225.     procedure SkelWhoa;
  1226.     begin
  1227.         doneFlag := 1;
  1228.     end;
  1229.  
  1230. {    Clobber all the menu, window and dialog handlers}
  1231.  
  1232.     procedure SkelClobber;
  1233.  
  1234.  
  1235.     begin
  1236.         oldWDHandler := nil;
  1237.         oldWindow := nil;
  1238.         while (whList <> nil) do
  1239.             begin
  1240.                 SkelRmveWind(whList^^.whWind);
  1241.             end;
  1242.         while (mhList <> nil) do
  1243.             begin
  1244.                 SkelRmveMenu(GetMHandle(mhList^^.mhID));
  1245.             end;
  1246.     end;
  1247.  
  1248. { -------------------------------------------------------------------- }
  1249. {                        Menu-handler interface routines                            }
  1250. { -------------------------------------------------------------------- }
  1251.  
  1252.  
  1253.  
  1254.  
  1255. {    Install handler for a menu.  Remove any previous handler for it.}
  1256. {    Pass the following parameters:}
  1257.  
  1258. {    theMenu    Handle to the menu to be handled.  Must be created by host.}
  1259. {    pSelect    Proc that handles selection of items from menu.  If this is}
  1260. {            nil, the menu is installed, but nothing happens when items}
  1261. {            are selected from it.}
  1262. {    pClobber Proc for disposal of handler's data structures.  Usually}
  1263. {            nil for menus that remain in menu bar until program}
  1264. {            termination.}
  1265.  
  1266. {    The menu is installed and drawn in the menu bar.}
  1267.  
  1268. {     Return false if no handler could be allocated, true if successful. }
  1269.  
  1270.     function CommonSkelMenu (theMenu: MenuHandle; pSelect: ProcPtr; pClobber: ProcPtr): Boolean;
  1271.         var
  1272.             mh: MHandlerHnd;
  1273.             myHand: Handle;
  1274.     begin
  1275.         mhClobOnRmve := false;
  1276.         SkelRmveMenu(theMenu);
  1277.         mhClobOnRmve := true;
  1278.         myHand := NewHandle(Sizeof(MHandler));
  1279.         CommonSkelMenu := false;
  1280.         if myHand <> nil then
  1281.             begin
  1282.                 CommonSkelMenu := true;                    { show we really got the memory }
  1283.                 mh := MHandlerHnd(myHand);
  1284.                 mh^^.mhNext := mhList;
  1285.                 mhList := MHandlerHnd(myHand);
  1286.                 mh^^.mhID := theMenu^^.menuID;    { get menu id number }
  1287.                 mh^^.mhSelect := pSelect;            { install selection handler }
  1288.                 mh^^.mhClobber := pClobber;        { install disposal handler }
  1289.             end;
  1290.     end;
  1291.  
  1292. {  Install handler for a normal menu }
  1293.  
  1294.     function SkelMenu;
  1295.         var
  1296.             success: Boolean;
  1297.     begin
  1298.         success := CommonSkelMenu(theMenu, pSelect, pClobber);
  1299.         SkelMenu := success;
  1300.         if success then
  1301.             begin
  1302.                 InsertMenu(theMenu, 0);            { put menu at end of menu bar }
  1303.                 if DrawBar then
  1304.                     DrawMenuBar;
  1305.             end;
  1306.     end;
  1307.  
  1308. {    Install handler for a hiearcical menu.  Almost same as above.}
  1309. {    Added by Ingemar 22/8 -93}
  1310.  
  1311.     function SkelHMenu;
  1312.         var
  1313.             success: Boolean;
  1314.     begin
  1315.         success := CommonSkelMenu(theMenu, pSelect, pClobber);
  1316.         SkelHMenu := success;
  1317.         if success then
  1318.             begin
  1319.                 InsertMenu(theMenu, -1);            { put menu at end of menu bar }
  1320.             end;
  1321.     end;
  1322.  
  1323.  
  1324. {    Remove a menu handler.  This calls the handler's disposal routine}
  1325. {    and then takes the handler out of the handler list and disposes}
  1326. {    of it.}
  1327.  
  1328. {    Note that the menu MUST be deleted from the menu bar before calling}
  1329. {    the clobber proc, because the menu bar will end up filled with}
  1330. {    garbage if the menu was allocated with NewMenu (see discussion of}
  1331. {    DisposeMenu in Menu Manager section of Inside Macintosh).}
  1332.  
  1333.     procedure SkelRmveMenu;
  1334.  
  1335.         var
  1336.             mID: integer;
  1337.             h, h2: MHandlerHnd;
  1338.             p: ProcPtr;
  1339.             returnflag: Boolean;
  1340.  
  1341.     begin
  1342.         mID := theMenu^^.menuID;
  1343.         returnflag := false;
  1344.         if mhlist <> nil then
  1345.             begin
  1346.                 if mhList^^.mhID = mID then
  1347.                     begin
  1348.                         h2 := mhlist;
  1349.                         mhList := h2^^.mhNext;
  1350.                     end
  1351.                 else
  1352.                     begin
  1353.                         h := mhList;
  1354.                         while (h <> nil) and not returnflag do
  1355.                             begin
  1356.                                 h2 := h^^.mhNext;
  1357.                                 if (h2 = nil) then
  1358.                                     begin
  1359.                                         h := nil;
  1360.                                         returnflag := true;
  1361.                                     end
  1362.                                 else if h2^^.mhID = mID then
  1363.                                     begin
  1364.                                         h^^.mhNext := h2^^.mhNext;
  1365.                                         h := nil;
  1366.                                     end;
  1367.                                 if h <> nil then
  1368.                                     h := h2;
  1369.                             end;
  1370.                     end;
  1371.                 if not returnflag then
  1372.                     begin
  1373.                         DeleteMenu(mID);
  1374.                         DrawMenuBar;
  1375.                         p := h2^^.mhClobber;
  1376.                         if mhClobOnRmve and (p <> nil) then
  1377.                             callpMenu(theMenu, p);
  1378.                         DisposHandle(Handle(h2));
  1379.                     end;
  1380.             end;
  1381.     end;
  1382.  
  1383. {    Install a handler for the Apple menu.}
  1384.  
  1385. {    SkelApple is called if TransSkel is supposed to handle the apple}
  1386. {    menu itself.  The title is the title of the first item.  If nil,}
  1387. {    then only desk accessories are put into the menu.  If not nil, then}
  1388. {    the title is entered as the first item, followed by a gray line,}
  1389. {    then the desk accessories.}
  1390.  
  1391. {     SkelApple does not cause the menubar to be drawn, so if the Apple menu is the only menu, }
  1392. {    DrawMenuBar must be called afterward.}
  1393.  
  1394. {    No value is returned, unlike SkelMenu.  It is assumed that SkelApple will be called so early in the}
  1395. {    application that the call to SkelMenu is virtually certain to succeed.  }
  1396.  
  1397.     procedure SkelApple;
  1398.  
  1399.         var
  1400.             appleTitle: Str255;
  1401.             dummy: boolean;
  1402.     begin
  1403.         appleTitle := ' ';
  1404.         appleTitle[1] := char($14);
  1405.         appleID := 1;
  1406.         AppleMenu := NewMenu(appleID, appleTitle);
  1407.         if aboutTitle <> '' then
  1408.             begin
  1409.                 doAbout := true;
  1410.                 AppendMenu(appleMenu, aboutTitle);
  1411.                 AppendMenu(appleMenu, '(-');
  1412.                 AppleAboutProc := aboutProc;
  1413.             end;
  1414.         AddResMenu(appleMenu, 'DRVR');
  1415.         dummy := SkelMenu(appleMenu, @DoAppleItem, @DoAppleClobber, false);
  1416.     end;
  1417.  
  1418. { -------------------------------------------------------------------- }
  1419. {                    Window-handler interface routines                    }
  1420. { -------------------------------------------------------------------- }
  1421.  
  1422.  
  1423.  
  1424. {    Install handler for a window.  Remove any previous handler for it.}
  1425. {    Pass the following parameters:}
  1426.  
  1427. {    theWind    Pointer to the window to be handled.  Must be created by host.}
  1428. {    pMouse    Proc to handle mouse clicks in window.  The proc will be}
  1429. {            passed the point (in local coordinates), the time of the}
  1430. {            click, and the modifier flags word.}
  1431. {    pKey    Proc to handle key clicks in window.  The proc will be passed}
  1432. {            the character and the modifier flags word.}
  1433. {    pUpdate    Proc for updating window.  TransSkel brackets calls to update}
  1434. {            procs with calls to BeginUpdate and EndUpdate, so the visRgn}
  1435. {            is set up correctly.  A flag is passed indicating whether the}
  1436. {            window was resized or not.  BY CONVENTION, the entire portRect}
  1437. {            is invalidated when the window is resized.  That way, the}
  1438. {            handler's update proc can redraw the entire content region}
  1439. {            without interference from BeginUpdate/EndUpdate.  The flag}
  1440. {            is set to false after the update proc is called; the}
  1441. {            assumption is made that it will notice the resizing and}
  1442. {            respond appropriately.}
  1443. {    pActivate Proc to execute when window is activated or deactivated.}
  1444. {            A boolean is passed to it which is true if the window is}
  1445. {            coming active, false if it's going inactive.}
  1446. {    pClose    Proc to execute when mouse clicked in close box.  Useful}
  1447. {            mainly to temp window handlers that want to know when to}
  1448. {            self-destruct (with SkelRmveWind).}
  1449. {    pClobber Proc for disposal of handler's data structures}
  1450. {    pIdle    Proc to execute when no events are pending.}
  1451. {    frontOnly True if pIdle should execute on no events only when}
  1452. {            theWind is frontmost, false if executes all the time.  Note}
  1453. {            that if it always goes, everything else may be slowed down!}
  1454.  
  1455. {    If a particular procedure is not needed (e.g., key events are}
  1456. {    not processed by a handler), pass nil in place of the appropriate}
  1457. {    procedure address.}
  1458.  
  1459. {    Return false  if no handler could be allocated, true if successful.}
  1460.  
  1461.     function SkelWindow;
  1462.  
  1463.         var
  1464.             hHand: WhandlerHnd;
  1465.  
  1466.     begin
  1467.         whClobOnRmve := false;
  1468.         SkelRmveWind(theWind);
  1469.         whClobOnRmve := true;
  1470.  
  1471. {    Get new handler, attach to list of handlers.  It is attached to the beginning of the list, which is simpler;}
  1472. {    the order should be irrelevant to the hose, anyway. }
  1473.  
  1474.         hHand := WHandlerHnd(NewHandle(Sizeof(WHandler)));
  1475.         SkelWindow := false;
  1476.         if hHand <> nil then
  1477.             begin
  1478.                 hHand^^.whNext := whList;
  1479.                 whList := hHand;
  1480.                 with hHand^^ do
  1481.                     begin
  1482.                         SkelWindow := true;                    { Show that we got the memory }
  1483.                         whWind := theWind;
  1484.                         whMouse := pMouse;
  1485.                         whKey := pKey;
  1486.                         whUpdate := pUpdate;
  1487.                         whActivate := pActivate;
  1488.                         whClose := pClose;
  1489.                         whClobber := pClobber;
  1490.                         whIdle := pIdle;
  1491.                         whFrontOnly := frontOnly;
  1492.                         whSized := false;
  1493.                         whGrow := GrowRect;
  1494.                     end;
  1495.             end;
  1496.         SetPort(theWind); {Is this allowed for hidden windows? I thought so, but… /Ingemar, dec 93}
  1497.     end;
  1498.  
  1499. {    Remove a window handler.  This calls the handler's disposal routine}
  1500. {    and then takes the handler out of the handler list and disposes}
  1501. {    of it.}
  1502.  
  1503. {    SkelRmveWind is also called by SkelRmveDlog.}
  1504.  
  1505. {    Note that if the window cache variable is set to the window whose handler is being clobbered, the }
  1506. {    variable must be zeroed.    }
  1507.  
  1508.     procedure SkelRmveWind;
  1509.  
  1510.         var
  1511.             h, h2: WHandlerHnd;
  1512.             returnflag: Boolean;
  1513.  
  1514.     begin
  1515.         if theWind = oldWindow then
  1516.             begin
  1517.                 oldWindow := nil;
  1518. {•    oldWDHandler := nil;•}
  1519.             end;
  1520.  
  1521.         if (whList <> nil) then
  1522.             begin
  1523.                 returnflag := false;
  1524.                 if whList^^.whWind = theWind then
  1525.                     begin
  1526.                         h2 := whlist;
  1527.                         whList := whList^^.whNext;
  1528.                     end
  1529.                 else
  1530.                     begin
  1531.                         h := whList;
  1532.                         while (h <> nil) and not returnflag do
  1533.                             begin
  1534.                                 h2 := h^^.whNext;
  1535.                                 if (h2 = nil) then
  1536.                                     begin
  1537.                                         h := nil;
  1538.                                         returnflag := true;
  1539.                                     end
  1540.                                 else if h2^^.whWind = theWind then
  1541.                                     begin
  1542.                                         h^^.whNext := h2^^.whNext;
  1543.                                         h := nil;
  1544.                                     end;
  1545.                                 if h <> nil then
  1546.                                     h := h2;
  1547.                             end;
  1548.                     end;
  1549.                 if not returnflag then
  1550.                     begin
  1551.                         if (whClobOnRmve) then
  1552.                             DoClobber(h2);
  1553.                         DisposHandle(Handle(h2));
  1554.                     end;
  1555.             end;
  1556.     end;
  1557.  
  1558. {$IFC supportDialogs }
  1559.  
  1560. { -------------------------------------------------------------------- }
  1561. {                    Dialog-handler interface routines                                }
  1562. { -------------------------------------------------------------------- }
  1563.  
  1564.  
  1565.  
  1566. {    Install a dialog handler.  Remove any previous handler for it.}
  1567. {    SkelDialog calls SkelWindow as a subsidiary to install a window}
  1568. {    handler, then sets the event procedure on return.}
  1569.  
  1570. {    Pass the following parameters:}
  1571.  
  1572. {    theDialog    Pointer to the dialog to be handled.  Must be created}
  1573. {            by host.}
  1574. {    pEvent    Event-handling proc for dialog events.}
  1575. {    pClose    Proc to execute when mouse clicked in close box.  Useful}
  1576. {            mainly to dialog handlers that want to know when to}
  1577. {            self-destruct (with SkelRmveDlog).}
  1578. {    pClobber Proc for disposal of handler's data structures}
  1579.  
  1580. {    If a particular procedure is not needed, pass nil in place of}
  1581. {    the appropriate procedure address.}
  1582.  
  1583. {    Return false if no handler could be allocated, true if successful.}
  1584.  
  1585.     function SkelDialog;
  1586.  
  1587.         var
  1588.             wh: WHandlerHnd;
  1589.             aBool: Boolean;
  1590.  
  1591.     begin
  1592.         aBool := SkelWindow(theDialog, nil, nil, nil, nil, pClose, pClobber, nil, false);
  1593.         if aBool <> false then
  1594.             begin
  1595.                 wh := GetWDHandler(theDialog);
  1596.                 wh^^.whEvent := pEvent;
  1597. {Added by Ingemar 18/9 -93:}
  1598.                 wh^^.whFilter := pFilter; {Install a filter function to be called *before* IsDialogEvent!}
  1599.             end;
  1600.         SkelDialog := aBool;
  1601.     end;
  1602.  
  1603. {    Remove a dialog and its handler}
  1604.  
  1605.     procedure SkelRmveDlog;
  1606.  
  1607.     begin
  1608.         SkelRmveWind(theDialog);
  1609.     end;
  1610. {$ENDC}
  1611. { -------------------------------------------------------------------- }
  1612. {                    Miscellaneous interface routines                    }
  1613. { -------------------------------------------------------------------- }
  1614.  
  1615.  
  1616. {    Override the default sizing limits for a window, or, if theWind}
  1617. {    is nil, reset the default limits used by SkelWindow.}
  1618.  
  1619.     procedure SkelGrowBounds;
  1620.  
  1621.         var
  1622.             h: WHandlerHnd;
  1623.             r: Rect;
  1624.  
  1625.     begin
  1626.         if theWind = nil then
  1627.             SetRect(growRect, hLo, vLo, hHi, vHi)
  1628.         else
  1629.             begin
  1630.                 h := GetWHandler(theWind);
  1631.                 if h <> nil then
  1632.                     begin
  1633.                         SetRect(r, hLo, vLo, hHi, vHi);
  1634.                         h^^.whGrow := r;
  1635.                     end;
  1636.             end;
  1637.     end;
  1638.  
  1639. {    Set the event mask.}
  1640.  
  1641.     procedure SkelEventMask;
  1642.  
  1643.     begin
  1644.         eventMask := mask;
  1645.     end;
  1646.  
  1647. {    Return the event mask.}
  1648.  
  1649.     procedure SkelGetEventMask;
  1650.  
  1651.     begin
  1652.         mask := eventMask;
  1653.     end;
  1654.  
  1655. {    Install a background task.  If p is nil, the current task is}
  1656. {    disabled.}
  1657.  
  1658.     procedure SkelBackground;
  1659.  
  1660.     begin
  1661.         pBkgnd := p;
  1662.     end;
  1663.  
  1664. {    Return the current background task.  Return nil if none.}
  1665.  
  1666.     procedure SkelGetBackground;
  1667.     begin
  1668.         p := pBkgnd;
  1669.     end;
  1670.  
  1671. {    Install an event-inspecting hook.  If p is nil, the hook is}
  1672. {    disabled.}
  1673.  
  1674.     procedure SkelEventHook;
  1675.  
  1676.     begin
  1677.         pEvent := p;
  1678.     end;
  1679.  
  1680.     procedure SkelGetEventHook;
  1681.  
  1682.     begin
  1683.         p := pEvent;
  1684.     end;
  1685. {$IFC supportDialogs }
  1686.  
  1687. {    Set the mask for event types that will be passed to dialogs.}
  1688. {    Bit 1 is always set, so that null events will be passed.}
  1689. {    If this is not done, the caret does not blink in editText items.}
  1690.  
  1691.     procedure SkelDlogMask;
  1692.  
  1693.     begin
  1694.         dlogEventMask := BitOr(mask, 1);
  1695.     end;
  1696.  
  1697. {    Return the current dialog event mask.}
  1698.  
  1699.     procedure SkelGetDlogMask;
  1700.  
  1701.     begin
  1702.         mask := dlogEventMask;
  1703.     end;
  1704. {$ENDC}
  1705. end.